<template>
  <div>
    <div class="row-header">
      <el-form ref="searchForm" :model="searchForm" :inline="true" label-width="50px" >
        <el-form-item label=" " prop="startUserLoginName">
          <el-autocomplete class="inline-input" size="small" v-model="searchForm.startUserLoginName"
                           style="width: 300px;" :fetch-suggestions="querySearch" :trigger-on-focus="false"
                           @select="handleSelect" placeholder="请输入代理账号" clearable ></el-autocomplete>
        </el-form-item>
        <el-form-item label=" " label-width="80px" prop="dictFlowStatus">
          <el-select size="small" placeholder="请选择流程状态" style="width:300px" clearable v-model="searchForm.dictFlowStatus">
            <el-option v-for="item in dictFlowOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="row-form-btn">
        <el-button type="shenda" size="mini" icon="el-icon-search" @click="beforeinitData">查询</el-button>
        <el-button type="shenda" size="mini" icon="el-icon-refresh-right" @click="resetForm('searchForm')">重置</el-button>
      </div>
    </div>
    <div class="split-line"></div>
    <div class="row-main">
      <el-table
        :header-cell-style="{ 'background':$store.state.app.tableBgColor,'color':'#606266' }"
        :data="tableData"
        ref="table"
        tooltip-effect="dark"
        class="row-main-table"
        v-loading.fullscreen="tableLoading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.2)"
        @sort-change="changeSort"
      >
        <el-table-column
          sortable='custom'
          prop="id"
          label="流程号"
          align="center">
        </el-table-column>
        <el-table-column
          sortable='custom'
          prop="startUserLoginName"
          label="发起人"
          align="center">
        </el-table-column>
        <el-table-column
          sortable='custom'
          prop="cardNumberStart"
          label="起始业务号码"
          align="center"/>
        <el-table-column
          sortable='custom'
          prop="cardNumberEnd"
          label="结束业务号码"
          align="center"/>
        <el-table-column
          prop="dictFlowTypeName"
          label="流程类型"
          align="center"/>

        <el-table-column
          prop="dictFlowStatusName"
          label="流程状态"
          align="center">
          <template slot-scope="scope" style="color: red">
            <span v-if="scope.row.dictFlowStatus=='TASK_PENDING'"
                  style="color:red">{{ scope.row.dictFlowStatusName }}</span>
            <span v-if="scope.row.dictFlowStatus=='TASK_COMPLETED'" style="color: #409EFF">{{ scope.row.dictFlowStatusName }}</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="insertTimeDate"
          label="发起时间"
          align="center"/>

        <el-table-column
          label="操作"
          align="center">
          <template slot-scope="scope">
            <el-dropdown trigger="click">
              <div class="el-dropdown-link">
                <svg-icon class="el-dropdown-link" icon-class="operation"></svg-icon>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <el-button class="operation" style="width: 100%;" @click="toDetail(scope.row)">查看流程详情</el-button>
                </el-dropdown-item>
                <el-dropdown-item v-show="scope.row.dictFlowStatus=='TASK_PENDING'">
                  <el-button class="operation" style="width: 100%;" @click="toPass(scope.row)">强行通过</el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :page-count='maxPage'
        :total="totalPage"
        :page-sizes="[10,20,50,100]"
        background
        class="row-main-pager"></el-pagination>
    </div>
    <el-dialog class="delete" top="33vh" :visible.sync="stockOutDialog" :close-on-click-modal="false"
               :append-to-body="true" width="30%">
      <span style="display: flex; justify-content: center;" prop="num">点击确定后该流程会直接结束，该流程发起人申请的卡</span>
      <span style="display: flex; justify-content: center;" prop="num">会直接退回到其名下，确定操作吗</span>
      <span class="dialog-footer" slot="footer">
        <el-button @click="outStockCancel" size="small">取 消</el-button>
        <el-button type="primary" @click="toPassSave" size="small" v-preventReClick>确 定</el-button>
      </span>
    </el-dialog>
  </div>


</template>

<script>
  import {mapGetters} from 'vuex'
  import {
    getLoginName,
    examineFlowOem,
    inboundList, settingCardRealNameStatus, processHeadList

  } from '@/api/getData'

  export default {
    name: 'Home',
    data() {
      return {
        flowId: '',
        startCardNumber: '',
        endCardNumber: '',
        dialogVisible: false,
        isTruenameDialog: false,
        dialogVisible3: false,
        tableLoading: false,
        textarea1: '',
        radio2: '1',
        maxPage: 1,
        totalPage: 0,
        tableData: [],
        ascs: [],
        descs: [],
        pageSize: 10,
        currentPage: 1,
        id: '',
        insertTime: '',
        pid: '',
        dictDisabledCardStatus: '',
        dictDisabledCardStatusName: '',
        startCardNum: '',
        endCardNum: '',
        oprationEmp: '',
        oprationEmpName: '',
        dictOprationType: '',
        ruleTypeId: '',
        reason: '',
        oemId: '',
        startTime: '',
        endTime: '',
        ruleName: '',
        input: '',
        dictDisabledRuleType: '',
        dialogVisible1: false,
        value1: '',
        value2: '',
        stockOutDialog: false,
        dictFlowOptions: [
          {
            id: '',
            name: '请选择流程状态'
          },
          {
            id: 'TASK_PENDING',
            name: '进行中'
          },
          {
            id: 'TASK_COMPLETED',
            name: '完成'
          },
          {
            id: 'TASK_FAIL',
            name: '任务失败'
          },
        ],
        searchForm: {
          startUserLoginName: '',
          dictFlowStatus: ''
        },
        pagekey: '',
      }
    },

    created() {
      this.processHeadList()
    },
    methods: {
      // 搜索页数充值
      beforeinitData(loading) {
        this.currentPage = 1
        this.processHeadList(loading)
      },
      // 重置
      resetForm(formName) {
        // this.$refs[formName].resetFields();
        this.searchForm = {
          startUserLoginName: '',
          dictFlowStatus: ''
        }
        console.log(this.searchForm)

      },
      async querySearch(queryString, cb) {
        let a = await getLoginName({
          loginname: this.searchForm.startUserLoginName
        })
        if (a._result) {
          this.loginNames = [];
          for (let i = 0; i < a.data.length; i++) {
            this.loginNames.push({'value': a.data[i]})
          }
        }

        var loginNames = this.loginNames;
        //var results = queryString ? loginNames.filter(this.createFilter(queryString)) : loginNames;
        // 调用 callback 返回建议列表的数据
        cb(loginNames);
      },
      handleSelect() {

      },
      toDetail(flow) {
        this.$router.push({
          path: '/examine/examDetail',
          query: {
            flowId: flow.id,
            dictOemExamineFlag: flow.dictOemExamineFlag,
            dictFlowStatus: flow.dictFlowStatus
          }
        })
      },
      async toPass(row) {
        this.flowId = row.id
        this.stockOutDialog = true;
        this.pagekey = await this.$getPageKey();
      },
      outStockCancel() {
        this.stockOutDialog = false
      },
      async toPassSave() {
        const returnData = await examineFlowOem({
            id: this.flowId
          },
          {
            obj: this,
            key: 'tableLoading',
            pagekey: this.pagekey
          })
        if (returnData._result) {
          this.processHeadList()
          this.stockOutDialog = false
          this.$message.success("操作成功")
        }
      },
      changeSort(column) {
        //console.log(column);
        if (column.order == 'ascending') {
          this.descs = []
          this.ascs = [column.prop];
        } else {
          this.ascs = []
          this.descs = [column.prop];
        }
        this.processHeadList();
      },
      handleSelectionChange(val) {
        this.tableChecked = val
        const length = this.tableChecked.length
        //console.log(`共 ${length} 条`)
      },
      handleSizeChange(val) {
        //console.log(`每页 ${val} 条`)
        this.pageSize = val
        this.processHeadList()
      },
      handleCurrentChange(val) {
        //console.log(`当前页: ${val}`)
        this.currentPage = val
        this.processHeadList()
      },

      // 大流程列表
      async processHeadList() {
        this.tableLoading = true;
        const returnData = await processHeadList({
            startUserLoginName: this.searchForm.startUserLoginName,
            dictFlowStatus: this.searchForm.dictFlowStatus,
            dictFlowType: '',
            page: {
              current: this.currentPage + '',
              size: this.pageSize + '',
              ascs: this.ascs,
              descs: this.descs
            }
          },
          {
            obj: this,
            key: 'tableLoading'
          })
        this.tableData = []
        if (returnData._result) {
          this.tableLoading = false
          this.maxPage = Math.ceil(returnData.data.total / this.pageSize) // 分页的最大页数
          this.totalPage = returnData.data.total;
          // 数据回显
          returnData.data.records.map(item => {
            const obj = {}
            obj.dictOemExamineFlag = item.dictOemExamineFlag
            obj.dictOemExamineStatus = item.dictOemExamineStatus
            obj.page = item.page
            obj.oemId = item.oemId
            obj.dictFlowStatusName = item.dictFlowStatusName
            obj.dictFlowStatus = item.dictFlowStatus
            obj.dictFlowTypeName = item.dictFlowTypeName
            obj.dictFlowType = item.dictFlowType
            obj.id = item.id
            obj.insertTimeDate = item.insertTimeDate
            obj.insertTime = item.insertTime
            obj.insertTimeTime = item.insertTimeTime
            obj.startUserLoginName = item.startUserLoginName
            obj.cardNumberStart = item.cardNumberStart
            obj.cardNumberEnd = item.cardNumberEnd
            this.tableData.push(obj)
          })
          this.tableLoading = false
        }
      },
    },
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .disabledList-title {
    font-weight: bold;
    font-size: 14px;
    color: #606266;
    margin-right: 10px;
  }

  .disabledList-tableTitle {
    font-weight: bold;
    color: #606266;
  }

  .el-dropdown-link {
    font-size: 20px;
    cursor: pointer;
  }

  .el-popper[x-placement^=bottom] {
    margin-right: -51px;
    box-shadow: 0px 0px 10px #888888;
    margin-left: 5px;

    .operation {
      border: 0;
      outline: none;
      display: block;
      text-align: center;
      margin: 0;
    }
  }
</style>
